package com.ztesoft.web.taskgenerator.db.dao;

import java.lang.reflect.Method;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;

import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.support.SqlSessionDaoSupport;
import org.springframework.stereotype.Repository;

import com.ztesoft.core.common.Page;
import com.ztesoft.framework.exception.SysRuntimeException;
import com.ztesoft.framework.util.StringUtils;
import com.ztesoft.web.taskgenerator.db.arg.DmTaskStageArg;
import com.ztesoft.web.taskgenerator.db.arg.DmTaskStageArg.DmTaskStageCriteria;
import com.ztesoft.web.taskgenerator.db.dao.mapper.IDmTaskStageMapper;
import com.ztesoft.web.taskgenerator.db.po.DmTaskStagePO;

@Repository
public class DmTaskStageDao extends SqlSessionDaoSupport {

	@Resource(name = "majorSqlSessionTemplate")
	public void setSqlSessionTemplate(SqlSessionTemplate sqlSessionTemplate) {
		super.setSqlSessionTemplate(sqlSessionTemplate);
	}

	public List<Integer> countByArg(DmTaskStageArg arg) {
		return getMapper().countByArg(arg);
	}

	public int deleteByArg(DmTaskStageArg arg) {
		return getMapper().deleteByArg(arg);
	}

	public List<DmTaskStagePO> selectByArg(DmTaskStageArg arg) {
		return getMapper().selectByArg(arg);
	}

	/**
	 * 获取同一个任务里所有体检对象名称（需要体检的表名）
	 * 
	 * @param dmTaskId
	 * @return
	 */
	public List<DmTaskStagePO> getCheckObjNameByTaskId(Integer dmTaskId) {
		return getMapper().getCheckObjNameByTaskId(dmTaskId);
	}
	
	/**
	 * 根据任务id提取任务环节表清理环节记录 只取“数据恢复中”、“处理完成”或“失败”的清理环节记录
	 * 
	 * @param dmTaskId
	 * @return
	 */
	public List<DmTaskStagePO> selectForDataRecovery(Integer dmTaskId) {
		return getMapper().selectForDataRecovery(dmTaskId);
	}

	public int updateByArgSelective(DmTaskStagePO record, DmTaskStageArg arg) {
		return getMapper().updateByArgSelective(record, arg);
	}

	public int updateByArg(DmTaskStagePO record, DmTaskStageArg arg) {
		return getMapper().updateByArg(record, arg);
	}

	public Page<DmTaskStagePO> selectByArgAndPage(DmTaskStageArg arg, Page<DmTaskStagePO> resultPage) {
		List<DmTaskStagePO> resultList = getMapper().selectByArgAndPage(arg, resultPage);
		resultPage.setResultList(resultList);
		return resultPage;
	}

	public int insert(DmTaskStagePO record) {
		return getMapper().insert(record);
	}

	public int insertSelective(DmTaskStagePO record) {
		return getMapper().insertSelective(record);
	}

	public int insertBatch(List<DmTaskStagePO> records) {
		return getMapper().insertBatch(records);
	}

	public int deleteByPrimaryKey(Integer key) {
		return getMapper().deleteByPrimaryKey(key);
	}

	public DmTaskStagePO selectByPrimaryKey(Integer key) {
		return getMapper().selectByPrimaryKey(key);
	}

	public int updateByPrimaryKeySelective(DmTaskStagePO record) {
		return getMapper().updateByPrimaryKeySelective(record);
	}

	public int updateByPrimaryKey(DmTaskStagePO record) {
		return getMapper().updateByPrimaryKey(record);
	}

	public List<DmTaskStagePO> selectByTaskId(Integer dmTaskId) {
		return getMapper().selectByTaskId(dmTaskId);
	}

	public int updateByIdAndStage(DmTaskStagePO record) {
		return getMapper().updateByIdAndStage(record);
	}

	public int updateStageByDelayPause(Integer dmPlanId) {
		return getMapper().updateStageByDelayPause(dmPlanId);
	}

	public List<DmTaskStagePO> selectStageAndStrategy(Integer dmTaskId) {
		return getMapper().selectStageAndStrategy(dmTaskId);
	}

	public List<DmTaskStagePO> selectStage(Integer dmStrategyId, Integer dmTaskId) {
		return getMapper().selectStage(dmStrategyId, dmTaskId);
	}

	public List<DmTaskStagePO> selectNextStrategy(Integer dmTaskId, String state, Integer dmStrategyId) {
		return getMapper().selectNextStrategy(dmTaskId, state, dmStrategyId);
	}

	public Page<DmTaskStagePO> selectListByArgAndPage(DmTaskStageArg arg, Page<DmTaskStagePO> resultPage) {
		List<DmTaskStagePO> resultList = getMapper().selectByArgAndPage(arg, resultPage);
		resultPage.setResultList(resultList);
		return resultPage;
	}

	public Page<DmTaskStagePO> selectPageByArg(DmTaskStageArg arg, Page<DmTaskStagePO> resultPage) {
		List<DmTaskStagePO> resultList = getMapper().selectListByArgAndPage(arg, resultPage);
		resultPage.setResultList(resultList);
		return resultPage;
	}

	public List<DmTaskStagePO> selectListByArg(DmTaskStageArg arg, Page<DmTaskStagePO> resultPage) {
		List<DmTaskStagePO> resultList = getMapper().selectListByArgAndPage(arg, resultPage);
		return resultList;
	}

	public List<DmTaskStagePO> selectAllListByArg(DmTaskStageArg arg, Page<DmTaskStagePO> resultPage) {
		List<DmTaskStagePO> resultList = getMapper().selectAllListByArgAndPage(arg, resultPage);
		return resultList;
	}

	public Page<DmTaskStagePO> selectListInfo(Integer dmPlanId, String state, Page<DmTaskStagePO> resultPage) {
		List<DmTaskStagePO> resultList = getMapper().selectListInfo(dmPlanId, state, resultPage);
		resultPage.setResultList(resultList);
		return resultPage;
	}
	
	public DmTaskStagePO selectBySourceDataId(Integer sourceDataId) {
		return getMapper().selectBySourceDataId(sourceDataId);
	}
	
	public DmTaskStagePO selectStateCount(Integer dmTaskId) {
		return getMapper().selectStateCount(dmTaskId);
	}
	
	public List<DmTaskStagePO> selectCurrentMultiState(int dmTaskId, String[] stateArr) {
		//
		return getMapper().selectCurrentMultiState(dmTaskId, stateArr);

	}

	/**
	 * 查询任务环节的当前状态
	 * 
	 * @param dmTaskId
	 * @param state
	 * @return
	 */
	public List<DmTaskStagePO> selectCurrentState(Integer dmTaskId, String state) {
		return getMapper().selectCurrentState(dmTaskId, state);
	}

	/**
	 * 根据传入的Map条件进行查询，当前仅支持所有Map中Key字段的EqualTo查询
	 * 
	 * @param params
	 *            Map,Key=字段名，value=查询值
	 * @return
	 */
	public List<DmTaskStagePO> selectByMap(Map<String, Object> params) {
		return (selectByArg(buildQueryObject(params)));
	}

	private DmTaskStageArg buildQueryObject(Map<String, Object> params) {

		DmTaskStageArg arg = new DmTaskStageArg();
		DmTaskStageCriteria criteria = arg.createCriteria();

		Class criteriaClass = criteria.getClass();
		Set keys = params.keySet();

		if (keys != null) {

			Iterator iterator = keys.iterator();

			while (iterator.hasNext()) {

				Object key = iterator.next();
				Object value = params.get(key);
				for (Method method : criteriaClass.getMethods()) {
					if (method.getName().equals("and" + StringUtils.toUpperCaseFirstOne(key.toString()) + "EqualTo")) {
						try {
							method.invoke(criteria, value);
						} catch (Exception e) {
							throw new SysRuntimeException(e);
						}
						break;
					}
				}
			}
		}
		return arg;
	}

	public IDmTaskStageMapper getMapper() {
		return getSqlSession().getMapper(IDmTaskStageMapper.class);
	}

	public int updateStateAndStartTimeByPrimaryKey(Integer dmInfoId) {
		return getMapper().updateStateAndStartTimeByPrimaryKey(dmInfoId);
		
	}

	public int updateStateAndEndTimeByPrimaryKey(Integer dmInfoId,
			String backUpState) {
		return getMapper().updateStateAndEndTimeByPrimaryKey(dmInfoId,backUpState);
		
	}

}
